package com.vchello.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author power
 *
 */
public class DownXls {
	
	public static LinkedHashMap<String, String> contentTypeMap = new LinkedHashMap<String, String>();
	static{
		contentTypeMap.put("apk","application/apk");
		contentTypeMap.put("doc", "application/msword");
		contentTypeMap.put("docx", "application/msword");
		contentTypeMap.put("xls", "application/msexcel");
		contentTypeMap.put("xlsx", "application/msexcel");
		contentTypeMap.put("pdf", "application/pdf");
		contentTypeMap.put("zip", "application/zip");
		contentTypeMap.put("rar", "application/rar");
		contentTypeMap.put("txt", "application/txt");
		contentTypeMap.put("jar", "application/jar");
		contentTypeMap.put("chm", "application/mshelp");
		contentTypeMap.put("exe", "application/octet-stream");
		contentTypeMap.put("zip", "application/zip");
		contentTypeMap.put("xml", "text/xml");
		contentTypeMap.put("wml", "text/vnd.wap.wml");
		contentTypeMap.put("tiff", "image/tiff");
		contentTypeMap.put("tar", "application/x-tar");
		contentTypeMap.put("swf", "application/x-shockwave-flash");
		contentTypeMap.put("flv", "application/x-shockwave-flash");
		contentTypeMap.put("rtf", "text/rtf");
		contentTypeMap.put("ppt", "application/mspowerpoint");
		contentTypeMap.put("pptx", "application/mspowerpoint");
		contentTypeMap.put("png", "image/png");
		contentTypeMap.put("mpe", "video/mpeg");
		contentTypeMap.put("mp4", "video/mp4");
		contentTypeMap.put("mp2", "audio/mpeg");
		contentTypeMap.put("mime", "www/mime");
		contentTypeMap.put("mid", "audio/midi");
		contentTypeMap.put("lha", "application/octet-stream");
		contentTypeMap.put("js", "application/x-javascript");
		contentTypeMap.put("jpg", "image/jpeg");
		contentTypeMap.put("jpeg", "image/jpeg");
		contentTypeMap.put("jpe", "image/jpeg");
		contentTypeMap.put("gz", "application/x-gzip");
		contentTypeMap.put("css", "text/css");
		contentTypeMap.put("htm", "text/html");
		contentTypeMap.put("html", "text/html");
		contentTypeMap.put("gif", "image/gif");
	}

	/**
	 * 下载文件
	 * @param file
	 * @throws IOException 
	 */
	public static void downloadBp(File file,HttpServletRequest request,HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		BufferedInputStream br;
		try {
			br = new BufferedInputStream(new FileInputStream(file));
			byte[] buf = new byte[1024];
			int len = 0;
			
			response.reset(); // 非常重要

			response.setContentType("application/xls;charset=UTF-8");
			response.setHeader("Content-Disposition",
					"attachment; filename="+new String(file.getName().getBytes("gb2312"),"ISO8859-1"));
			OutputStream out = response.getOutputStream();
			while ((len = br.read(buf)) > 0)
				out.write(buf, 0, len);
			br.close();
			out.flush();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 根据文件获取后缀名，然后得到ContentType
	 * @param fileName
	 * @return
	 */
	public static String getContentTypeByFileName(String fileName){
		String contentType="";
		try {
			String extension = fileName.replaceAll(".*\\.", "");
			for (Map.Entry<String, String> m : contentTypeMap.entrySet()) {
				   if(m.getKey().equalsIgnoreCase(extension)){
					   contentType=m.getValue();
				   }
			   }
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return contentType;
	}

}
